package co.yixiang.modules.shop.web.controller;

import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.common.api.ApiResult;
import co.yixiang.common.web.controller.BaseController;
import co.yixiang.modules.shop.entity.YxStoreProductCheckmake;
import co.yixiang.modules.shop.service.YxStoreProductCheckmakeService;
import co.yixiang.modules.shop.service.YxStoreProductService;
import co.yixiang.modules.shop.web.param.YxStoreProductCheckmakeQueryParam;
import co.yixiang.modules.shop.web.param.YxStoreProductCheckmakeSaveParam;
import co.yixiang.modules.shop.web.vo.YxStoreProductCheckmakeQueryVo;
import co.yixiang.modules.shop.web.vo.YxStoreProductQueryVo;
import co.yixiang.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 盘点系统控制器
 * </p>
 *
 * @author frq
 * @since 2020-07-16
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "盘点系统", description = "盘点系统", tags = "盘点系统")
public class InventorySystemController extends BaseController {

    @Autowired
    private YxStoreProductCheckmakeService yxStoreProductCheckmakeService;
    @Autowired
    private YxStoreProductService yxStoreProductService;

    /**
     * 根据条码查询商品
     *
     * @param barCode
     * @return
     */
    @AnonymousAccess
    @GetMapping("/inventorySystem/findGoogsInformat")
    @ApiOperation(value = "查询商品信息", notes = "查询商品信息")
    public ApiResult findByBarCodeGoogs(@RequestParam String barCode) {
        YxStoreProductQueryVo queryVo = yxStoreProductService.findByBarCodeGoogs(barCode);
        return ApiResult.ok(queryVo);
    }

    @PostMapping("/inventorySystem/saveInventory")
    @ApiOperation(value = "保存盘点信息", notes = "保存盘点信息")
    public ApiResult saveInventory(@RequestBody YxStoreProductCheckmakeSaveParam saveParam) {
        YxStoreProductCheckmake yxStoreProductCheckmake = new YxStoreProductCheckmake();
        yxStoreProductCheckmake.setAdder(SecurityUtils.getUsername());
        yxStoreProductCheckmake.setAddTime(new Date());
        yxStoreProductCheckmake.setBarCode(saveParam.getBarCode());
        yxStoreProductCheckmake.setProductName(saveParam.getProductName());
        yxStoreProductCheckmake.setStock(saveParam.getStock());
        yxStoreProductCheckmake.setShelfNum(saveParam.getShelfNum());
        boolean flag = yxStoreProductCheckmakeService.save(yxStoreProductCheckmake);
        if (flag) {
            return ApiResult.ok("保存成功");
        } else {
            return ApiResult.ok("保存失败");
        }
    }
    @AnonymousAccess
    @PostMapping("/inventorySystem/findInventory")
    @ApiOperation(value = "查询盘点信息",notes = "查询盘点意思")
    public ApiResult findInventory(@RequestBody YxStoreProductCheckmakeQueryParam param){
        List<YxStoreProductCheckmakeQueryVo> yxStoreProductCheckmake=yxStoreProductCheckmakeService.findInventory(param);
        return ApiResult.ok(yxStoreProductCheckmake);
    }
}

